package com.gzs.learn.admin.web.controller.api;

import com.gzs.learn.admin.clients.ISerialQueryClient;
import com.gzs.learn.common.AjaxResult;
import com.gzs.learn.common.util.Base32;
import com.gzs.learn.common.util.Base62;
import com.gzs.learn.inf.BaseResponseDto;
import com.gzs.learn.serial.inf.dto.SerialSnowflakeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/serial")
@Api("序列号服务")
@RequiredArgsConstructor
public class SerialApiController {
    private final ISerialQueryClient serialQueryClient;

    @GetMapping(value = "/segment")
    @ApiOperation("获取指定key值的流水号")
    public AjaxResult getSegmentId(String key) {
        BaseResponseDto<Long> segmentResponse = serialQueryClient.getSegmentId(key);
        return AjaxResult.success(segmentResponse);
    }

    @GetMapping(value = "/snowflake")
    @ApiOperation("获取snowflake流水号")
    public AjaxResult getSnowflakeId(String key) {
        BaseResponseDto<Long> snowflakeResponse = serialQueryClient.getSnowflakeId();
        return AjaxResult.success(snowflakeResponse);
    }

    @GetMapping(value = "/snowflake/decode")
    @ApiOperation("snowflake流水号解码")
    public AjaxResult decode(Long id) {
        BaseResponseDto<SerialSnowflakeInfo> decodeResponse = serialQueryClient.decodeSnowflake(id);
        return AjaxResult.success(decodeResponse);
    }

    @GetMapping(value = "/base32/decode")
    @ApiOperation("base32解码")
    public AjaxResult decodeBase32(String id) {
        long result = Base32.decodeBase32ToLong(id);
        return AjaxResult.success(result);
    }

    @GetMapping(value = "/base32/encode")
    @ApiOperation("base32编码码")
    public AjaxResult encodeBase32(Long id) {
        String result = Base32.encodeLongToBase32(id);
        return AjaxResult.success("success", result);
    }

    @GetMapping(value = "/base62/decode")
    @ApiOperation("base62解码")
    public AjaxResult decodeBase64(String id) {
        long result = Base62.toBase10(id);
        return AjaxResult.success(result);
    }

    @GetMapping(value = "/base62/encode")
    @ApiOperation("base62编码")
    public AjaxResult encodeBase64(Long id) {
        String result = Base62.fromBase10(id);
        return AjaxResult.success("success", result);
    }
}
